package com.tridion.utilities.verifier.analyzer;

import java.io.File;

import com.tridion.utilities.verifier.model.ContentDeliveryInstallation;
import com.tridion.utilities.verifier.model.InstallationType;
import com.tridion.utilities.verifier.utils.Constants;

/**
 * Analyzer that checks the type of installation we have on our hands.
 * 
 * @author Daniel Neagu
 */
public class InstallationTypeAnalyzer implements Analyzer {

    /**
     * Makes a basic test to find-out what type of installation we have on our hands and sets 
     * the right property from the {@link ContentDeliveryInstallation}.
     */
    @Override
    public void analyze(final ContentDeliveryInstallation cdi) {
        final File rootFolder = cdi.getRootLocation();
        
        // check all possibilities to find the lib folder
        // TODO: take into account also WAR, EAR files and .NET installation where libs are in TRIDION_HOME rather than inside the installation
        final File javaWebAppLib = new File(rootFolder, Constants.JAVA_WEB_APP_LIB_FOLDER);
        final File dotNetWebAppLib = new File(rootFolder, Constants.DOT_NET_WEB_APP_LIB_FOLDER);
        final File winServiceLib = new File(rootFolder, Constants.WINDOWS_SERVICE_LIB_FOLDER);
        
        if (javaWebAppLib.exists() && javaWebAppLib.isDirectory()) {
            cdi.setType(InstallationType.JAVA_WEB_APPLICATION);
        } else if (dotNetWebAppLib.exists() && dotNetWebAppLib.isDirectory()) {
            cdi.setType(InstallationType.DOT_NET_WEB_APPLICATION);
        } else if (winServiceLib.exists() && winServiceLib.isDirectory()) {
            cdi.setType(InstallationType.DOT_NET_WEB_APPLICATION);
        } else {
            cdi.setType(InstallationType.UNKNOWN_TYPE);
        }
    }
}
